package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"ginTea/utils/validator"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 添加用户
func AddUser(c *gin.Context) {
	var data model.User
	var msg string
	var code int
	var success bool
	_ = c.ShouldBindJSON(&data)
	//数据验证
	msg, code = validator.Validate(&data)
	if code != errmsg.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  msg,
		})
		return
	}
	// 检测用户名是否已经存在
	code = model.CheckUserExist(data.Username)
	if code == errmsg.SUCCESS {
		//创建用户
		model.CreateUser(&data)
		success = true
	}
	if code == errmsg.ERROR_USERNAME_USED {
		code = errmsg.ERROR_USERNAME_USED
		success = false
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": success,
	})
}

// 删除用户
func DeleteUser(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var code int
	code = model.DeleteUser(id)
	if code == errmsg.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	}

}

// 获取用户列表
func GetUserList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	page, _ := strconv.Atoi(c.Query("page"))
	keyword := c.Query("keyword")
	types := c.Query("type")
	if pageSize == 0 {
		pageSize = 10
	}
	if page == 0 {
		page = 1
	}
	data := model.GetUserList(page, pageSize, keyword, types)
	c.JSON(http.StatusOK, data)
}

// 获取用户信息
func GetUserInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	data, code := model.GetUserInfo(id)
	if code == errmsg.ERROR_GOODS_NOT_EXIST {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"data":    data,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"data":    data[0],
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}

}

// 编辑用户
func UpdateUser(c *gin.Context) {
	var data model.User
	var code int
	c.ShouldBindJSON(&data)
	if data.ID > 0 {
		code = model.UpdateUser(&data)
	} else {
		code = errmsg.ERROR_USER_NOT_EXIST
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
		return
	}
	if code == errmsg.ERROR_USERNAME_USED {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}
func UpdateUserByCus(c *gin.Context) {
	var data model.User
	var code int
	c.ShouldBindJSON(&data)
	if data.ID > 0 {
		code = model.UpdateUserByCus(&data)
	} else {
		code = errmsg.ERROR_USER_NOT_EXIST
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
		return
	}
	if code == errmsg.ERROR_USERNAME_USED {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}

// 修改密码
func UpdatePwd(c *gin.Context) {
	var code int
	var data model.User
	c.ShouldBindJSON(&data)
	code = model.UpdatePwd(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}
